﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using GameEditor.Common;
using GameEditor.Common.Controls;

namespace GameEditor.Common
{
    /// <summary>
    /// 新增的支持颜色值大于255的颜色选取器
    /// </summary>
    partial class ColourPicker : Form
    {
        WhitenessPicker whitenessPicker;
        HueBlacknessPicker hueBlacknessPicker;
        AlphaPicker alphaPicker;
        ColorPreview preview;

        SimpleMutex operationMutex = new SimpleMutex();
        SimpleMutex rgbMutex = new SimpleMutex();
        SimpleMutex floatRGBMutex = new SimpleMutex();
        SimpleMutex alphaMutex = new SimpleMutex();
        SimpleMutex alphaFMutex = new SimpleMutex();

        public ColourPicker()
        {
            InitializeComponent();
            whitenessPicker = new WhitenessPicker();
            Controls.Add(whitenessPicker);
            whitenessPicker.Location = locator_Whiteness.Location;
            whitenessPicker.Size = locator_Whiteness.Size;
            whitenessPicker.ColorChanged += new EventHandler(whitenessPicker_ColorChanged);

            hueBlacknessPicker = new HueBlacknessPicker();
            Controls.Add(hueBlacknessPicker);
            hueBlacknessPicker.Location = locator_HueMap.Location;
            hueBlacknessPicker.Size = locator_HueMap.Size;
            hueBlacknessPicker.ColorChanged += new EventHandler(hueBlacknessPicker_ColorChanged);
            
            alphaPicker = new AlphaPicker();
            Controls.Add(alphaPicker);
            alphaPicker.Location = locator_alpha.Location;
            alphaPicker.Size = locator_alpha.Size;
            alphaPicker.ValueChanged += new EventHandler(alphaPicker_ValueChanged);


            preview = new ColorPreview();
            Controls.Add(preview);
            preview.Location = locator_preview.Location;
            preview.Size = locator_preview.Size;

        }

        void alphaPicker_ValueChanged(object sender, EventArgs e)
        {
            alpha.Value = alphaPicker.Alpha;
            alphaF.Value = (decimal)(alphaPicker.Alpha / 255.0f);
        }

        private void alpha_ValueChanged(object sender, EventArgs e)
        {
            if (alphaMutex.Open())
            {
                alphaPicker.Alpha = (int)alpha.Value;
                alphaPicker.Invalidate();
                alphaMutex.Release();
            }
            if (alphaFMutex.Open())
            {
                alphaF.Value = (decimal)((int)alpha.Value / 255.0f);
                alphaFMutex.Release();
            }
            ColorChanged();
        }
        private void alphaF_ValueChanged(object sender, EventArgs e)
        {
            if (alphaFMutex.Open())
            {
                alphaPicker.Alpha = (int)(255 * (float)alphaF.Value);
                alphaPicker.Invalidate();
                alphaFMutex.Release();
            }
            if (alphaMutex.Open())
            {
                alpha.Value = (int)(255 * (float)alphaF.Value);
                alphaMutex.Release();
            }
            ColorChanged();

        }

        void whitenessPicker_ColorChanged(object sender, EventArgs e)
        {
            if (operationMutex.Open())
            {
                Color color = whitenessPicker.Color;
                ColourValue c = new ColourValue(color);
                //update colors
                updateTextBoxes(c);


                ColorChanged();

                operationMutex.Release();
            }
        }


        void updateTextBoxes(ColourValue color)
        {
            if (rgbMutex.Open())
            {
                red.Value = color.Red;
                green.Value = color.Green;
                blue.Value = color.Blue;
                rgbMutex.Release();
            }
            if (floatRGBMutex.Open())
            {
                redF.Value = (decimal)color.FRed;
                greenF.Value = (decimal)color.FGreen;
                blueF.Value = (decimal)color.FBlue;
                floatRGBMutex.Release();
            }
        }

        private void ColorChanged()
        {
            int alpha = alphaPicker.Alpha > 255 ? 255 : alphaPicker.Alpha;
            Color newColor = Color.FromArgb(alpha, whitenessPicker.Color);

            //newColor.BackColor = whitenessPicker.Color; 

            //uint _a = (uint)(a * 255);
            //uint _r = (uint)(r * 255);
            //uint _g = (uint)(g * 255);
            //uint _b = (uint)(b * 255);
            //UInt32 c = (_a << 24) | (_r << 16) | (_g << 8) | _b;
            htmlColor.Text = "#"+newColor.Name.ToUpper();
            preview.Color1 = newColor;
            preview.Invalidate();
            alphaPicker.GenerateColor(newColor);
            alphaPicker.Invalidate();

        }

        void hueBlacknessPicker_ColorChanged(object sender, EventArgs e)
        { 
            whitenessPicker.GenerateWhitenessMap(hueBlacknessPicker.CurrentColor);
            whitenessPicker.Invalidate();


                //operationMutex.Release(); 
        }

        private void ColourPicker_Load(object sender, EventArgs e)
        {

        }

        public DialogResult ShowDialog(ref ColourValue c)
        {
            preview.Color2 = c.ToColor();

            red.Value = c.Red;
            green.Value = c.Green;
            blue.Value = c.Blue;
            alpha.Value = c.Alpha;
            

            ApplyColor(c);
            DialogResult dr = ShowDialog();
            if (dr == DialogResult.OK)
            {
                
                c = ColourValue.FromRGBA_0_1(
                    (float)this.redF.Value,
                    (float)this.greenF.Value,
                    (float)this.blueF.Value,
                    (float)this.alphaF.Value);               
            }
            return dr;

        }



        private void ApplyColor(ColourValue c)
        {

            if (operationMutex.Open())
            {
                ColourValue color = new ColourValue(c.ToColor());
                HSB hsv = HSB.FromRGB(color);
                //�����
                whitenessPicker.Whiteness = 0;
                float blackness = (c.Red + c.Green + c.Blue) / 3.0f;
                hueBlacknessPicker.Blackness = 1 - blackness;
                hueBlacknessPicker.Hue = hsv.Hue;
                hueBlacknessPicker.Invalidate();
                whitenessPicker.GenerateWhitenessMap(c.ToColor());

                ColorChanged();
                operationMutex.Release();
            }
            updateTextBoxes(c);
        }

        private void RGBChanged(object sender, EventArgs e)
        {
            if (rgbMutex.Open())
            {
                ColourValue c = new ColourValue(
                    (int)red.Value / 255.0f,
                    (int)green.Value / 255.0f,
                    (int)blue.Value / 255.0f, 1);
                ApplyColor(c);

                rgbMutex.Release();
            }

        }

        private void FloatRGBChanged(object sender, EventArgs e)
        {
            if (floatRGBMutex.Open())
            {
                ColourValue c = new ColourValue(
                    (float)redF.Value,
                    (float)greenF.Value,
                    (float)blueF.Value, 1);
                ApplyColor(c);

                floatRGBMutex.Release();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

        }

    }


  


 
}